<template>
  <a-drawer
    :title="title"
    :width="width"
    placement="right"
    :closable="false"
    @close="close"
    :visible="visible">
  
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="作者" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'author', validatorRules.author]" placeholder="请输入作者"></a-input>
        </a-form-item>
        <a-form-item label="剧本名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'productName', validatorRules.productName]" placeholder="请输入剧本名称"></a-input>
        </a-form-item>
        <a-form-item label="生产时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择生产时间" v-decorator="[ 'productCreattime', validatorRules.productCreattime]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="剧本简介 " :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'productInfo', validatorRules.productInfo]" placeholder="请输入剧本简介 "></a-input>
        </a-form-item>
        <a-form-item label="剧本图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-upload v-decorator="['productImages', validatorRules.productImages]" :trigger-change="true"></j-upload>
        </a-form-item>
        <a-form-item label="零售价格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'wholesalePrice', validatorRules.wholesalePrice]" placeholder="请输入零售价格"></a-input>
        </a-form-item>
        <a-form-item label="规格编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'productStandardsId', validatorRules.productStandardsId]" placeholder="请输入规格编号"></a-input>
        </a-form-item>
        <a-form-item label="剧本编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'productId', validatorRules.productId]" placeholder="请输入剧本编号"></a-input>
        </a-form-item>
        <a-form-item label="详细介绍" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'detailInfo', validatorRules.detailInfo]" placeholder="请输入详细介绍"></a-input>
        </a-form-item>
        <a-form-item label="登记时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择登记时间" v-decorator="[ 'registerTime', validatorRules.registerTime]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="物品码载体" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-multi-select-tag type="checkbox" v-decorator="['itemCode', validatorRules.itemCode]" :trigger-change="true" dictCode="product_loader" placeholder="请选择物品码载体"/>
        </a-form-item>
        <a-form-item label="标签" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-multi-select-tag type="checkbox" v-decorator="['labelCode', validatorRules.labelCode]" :trigger-change="true" dictCode="product_label" placeholder="请选择标签"/>
        </a-form-item>
        
      </a-form>
    </a-spin>
    <a-button type="primary" @click="handleOk">确定</a-button>
    <a-button type="primary" @click="handleCancel">取消</a-button>
  </a-drawer>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  
  import JUpload from '@/components/jeecg/JUpload'
  import JMultiSelectTag from "@/components/dict/JMultiSelectTag"
  
  export default {
    name: "BlockProductInfoModal",
    components: { 
      JDate,
      JUpload,
      JMultiSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          author: {rules: [
            {required: true, message: '请输入作者!'},
          ]},
          productName: {rules: [
            {required: true, message: '请输入剧本名称!'},
          ]},
          productCreattime: {rules: [
            {required: true, message: '请输入生产时间!'},
          ]},
          productInfo: {rules: [
            {required: true, message: '请输入剧本简介 !'},
          ]},
          productImages: {rules: [
          ]},
          wholesalePrice: {rules: [
          ]},
          productStandardsId: {rules: [
            {required: true, message: '请输入规格编号!'},
          ]},
          productId: {rules: [
            {required: true, message: '请输入剧本编号!'},
          ]},
          detailInfo: {rules: [
          ]},
          registerTime: {rules: [
          ]},
          itemCode: {rules: [
            {required: true, message: '请输入物品码载体!'},
          ]},
          labelCode: {rules: [
            {required: true, message: '请输入标签!'},
          ]},
        },
        url: {
          add: "/block/blockProductInfo/add",
          edit: "/block/blockProductInfo/edit",
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'author','productName','productCreattime','productInfo','productImages','wholesalePrice','productStandardsId','productId','detailInfo','registerTime','itemCode','labelCode'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'author','productName','productCreattime','productInfo','productImages','wholesalePrice','productStandardsId','productId','detailInfo','registerTime','itemCode','labelCode'))
      }
      
    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
</style>